package com.hengxinguotong.zhihuichengjian.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.adapter.AddTeamAdapter;
import com.hengxinguotong.zhihuichengjian.bean.Team;
import com.hengxinguotong.zhihuichengjian.bean.TeamRes;
import com.hengxinguotong.zhihuichengjian.utils.SPUtil;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;
import com.lidroid.xutils.http.RequestParams;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemClick;

/**
 * Created by Administrator on 2017/9/27.
 */

public class TeamListActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener{

    @BindView(R.id.tv_back)
    HXTextView tvBack;
    @BindView(R.id.tv_title)
    HXTextView tvTitle;
    @BindView(R.id.tv_detail)
    HXTextView tvDetail;
    @BindView(R.id.team_lv)
    ListView teamLv;

    private AddTeamAdapter adapter;
    private List<Team> mTeamList;
    private List<Team> mSelectedTeamList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_team_list);
        ButterKnife.bind(this);
        if(mSelectedTeamList == null){
            mSelectedTeamList = new ArrayList<>();
        }
        getTeamList("加载中…");
    }

    private void getTeamList(String alert) {
        JSONObject jsonObject = new JSONObject();

        try {
            jsonObject.put("constructionId", SPUtil.getString(this, "constructionid"));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        RequestParams params = new RequestParams();
        params.addHeader("Content-type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Utils.requestData(alert, this, "/team/selectTeamList/" + SPUtil.getString(this, "uuid"), null, new Utils.ListenerJson() {
            @Override
            public void onSuccess(String json) {
                if (!Utils.isEmpty(json)) {
                    Gson gson = new Gson();
                    TeamRes teamRes = gson.fromJson(json, TeamRes.class);
                    mTeamList = teamRes.getValue();
                    process();
                }
            }

            @Override
            public void onFailure(String json) {

            }
        });
    }

    private void process(){
        adapter = new AddTeamAdapter(this, mTeamList);
        teamLv.setAdapter(adapter);
    }

    @OnClick({R.id.tv_back, R.id.tv_detail})
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_back:
                finish();
                break;
            case R.id.tv_detail:
                if (mSelectedTeamList.size() <= 0) {
                    showToast("请至少选择一组");
                } else {
                    Intent intent = getIntent();
                    intent.putExtra("teamList", (Serializable) mSelectedTeamList);
                    setResult(2, intent);
                    finish();
                }
                break;
        }
    }

    @OnItemClick({R.id.team_lv})
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Team team = mTeamList.get(position);
        if(mSelectedTeamList.contains(team)){
            mSelectedTeamList.remove(team);
        }else{
            mSelectedTeamList.add(team);
        }
        adapter.notifyDataSetChanged();
    }

    public List<Team> getmSelectedTeamList() {
        return mSelectedTeamList;
    }
}
